//
//  CYMessageNotificationViewController.m
//  cyydt
//
//  Created by chu on 2019/1/21.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYMessageNotificationViewController.h"
#import "CYMessageNotificationTableViewCell.h"
#import "CYMessageNotifiModel.h"
#import "CYArcitleDetailsViewController.h"
#import "CYProjectDetailViewController.h"
#import "CYHomeVideoDetailsViewController.h"
#import "CYWenDaDetailViewController.h"

#import "CYPublishVideoViewController.h"
#import "RichTextEditViewController.h"
#import "CYPubilishQuestionViewController.h"
@interface CYMessageNotificationViewController ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@end

@implementation CYMessageNotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 44;
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self getData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CYMessageNotificationTableViewCell *cell = [CYMessageNotificationTableViewCell initWithTableView:tableView Identifier:@"CYMessageNotificationTableViewCell"];
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    CYMessageNotificationTableViewCell *cell = [CYMessageNotificationTableViewCell initWithTableView:tableView Identifier:@"CYMessageNotificationTableViewCell"];
//    cell.model = self.dataSourceArr[indexPath.row];
//    return cell.cellHeight;
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CYMessageNotifiModel *model = self.dataSourceArr[indexPath.row];
    if ([model.type isEqualToString:@"3"]) {
        //项目
        CYProjectDetailViewController *detail = [[CYProjectDetailViewController alloc] init];
        NSDictionary *dic = @{@"id":model.articleId};
        CYProjectModel *proModel = [CYProjectModel mj_objectWithKeyValues:dic];
        detail.model = proModel;
        [[AppDelegate sharedAppDelegate] pushViewController:detail];
    }else if ([model.type isEqualToString:@"4"]){
        //问答
        if ([model.cateId integerValue] == 5) {
            //被驳回
            CYPubilishQuestionViewController *wenda = [[CYPubilishQuestionViewController alloc] init];
            wenda.wentiId = [model.articleId stringValue];
            wenda.type = TiWenType_BoHui;
            [[AppDelegate sharedAppDelegate] pushViewController:wenda];

        }else{
            CYWenDaDetailViewController *detail = [[CYWenDaDetailViewController alloc] init];
            detail.wedaId = [model.articleId stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:detail];
        }
    }else if ([model.type isEqualToString:@"28"]){
        //视频
        if ([model.cateId integerValue] == 5) {
            //被驳回
            CYPublishVideoViewController *rich = [[CYPublishVideoViewController alloc] init];
            rich.questionId = [model.articleId stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:rich];
        }else{
            CYHomeVideoDetailsViewController *detail = [[CYHomeVideoDetailsViewController alloc] init];
            detail.videoId = [model.articleId stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:detail];

        }
        
    }else if ([model.type isEqualToString:@"7"]){
        //咨询
        if ([model.cateId integerValue] == 5) {
            //被驳回
            RichTextEditViewController *rich = [[RichTextEditViewController alloc] init];
            rich.articleId = [model.articleId stringValue];
            rich.from = ResourceFrom_WeiTongGuo;
            [[AppDelegate sharedAppDelegate] pushViewController:rich];
        }else{
            CYArcitleDetailsViewController *detail = [[CYArcitleDetailsViewController alloc] init];
            detail.articleId = [model.articleId stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:detail];
        }
        
    }else{
        if ([model.cateId integerValue] == 4) {
            //关注
            CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
            zhuye.userId = model.fromUserid;
            [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
        }
    }
}

#pragma mark - DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"wuneir"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"当前页面没有内容！";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:RGBOF(0x999999)
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    // 空白页面被点击时开启动画，reloadEmptyDataSet
    [self getData];
}

- (void)getData{
    self.page = 1;
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"message/informs"];
    NSDictionary *param = @{@"pageNo":@1};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"精选列表 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        [self.tableView removeFromSuperview];
        [self.view addSubview:self.tableView];
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSArray *data = responseResult[@"data"];
                [self.dataSourceArr removeAllObjects];
                for (NSDictionary *dic in data) {
                    CYMessageNotifiModel *model = [CYMessageNotifiModel mj_objectWithKeyValues:dic];
                    [self.dataSourceArr addObject:model];
                }
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
        [self.tableView reloadData];

    }];
}

- (void)getMoreData{
    self.page ++;
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"message/informs"];
    NSDictionary *param = @{@"pageNo":[NSNumber numberWithInteger:self.page]};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"精选列表 ---- %@",responseResult);
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
            self.page -- ;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSArray *data = responseResult[@"data"];
                for (NSDictionary *dic in data) {
                    CYMessageNotifiModel *model = [CYMessageNotifiModel mj_objectWithKeyValues:dic];
                    [self.dataSourceArr addObject:model];
                }
                [self.tableView reloadData];
            }else{
                self.page -- ;
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar - 44) style:UITableViewStyleGrouped];
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        [_tableView registerNib:[UINib nibWithNibName:@"CYMessageNotificationTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYMessageNotificationTableViewCell"];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self getMoreData];
        }];
        
    }
    return _tableView;
}

- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}

@end
